package com.example.umengdemo.utils

import android.content.Context
import android.os.Bundle
import com.example.umengdemo.utils.Constant.Companion.UMENG_SEND_TIME
import com.example.umengdemo.utils.Constant.Companion.UM_APP_CHANNEL
import com.example.umengdemo.utils.Constant.Companion.UM_APP_KEY
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.BuildConfig
import com.umeng.commonsdk.UMConfigure
import com.umeng.umcrash.UMCrash

/**
 * @auth: njb
 * @date: 2021/10/21 15:06
 * @desc: 友盟工具类
 */
class UmengUtils private constructor() {

    /**
     * 友盟初始化方法
     */
    fun init(context: Context) {
        //集成Umeng崩溃
        val bundle = Bundle()
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_JAVA, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_NATIVE, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_UNEXP, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_ANR, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_PA, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_LAUNCH, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_MEM, false)
        UMCrash.initConfig(bundle)
        //设置友盟log开关,默认为false
        UMConfigure.setLogEnabled(BuildConfig.DEBUG)
        //初始化组件化基础库, 统计SDK
        UMConfigure.init(context, UM_APP_KEY, UM_APP_CHANNEL, UMConfigure.DEVICE_TYPE_PHONE, null)
        //设置统计模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
        //设置发送策略时间为40秒
        MobclickAgent.setSessionContinueMillis((UMENG_SEND_TIME).toLong())
    }

    companion object {
        @Volatile
        var mInstance: UmengUtils? = null

        fun getInstance(): UmengUtils? {
            if (mInstance == null) {
                synchronized(UmengUtils::class.java) {
                    if (mInstance == null) {
                        mInstance = UmengUtils()
                    }
                }
            }
            return mInstance
        }
    }
}